#include "db.h"

#include "in.h"

int press_insert_records( uint32_t press_count )
{
	struct MemDatabase	*db = NULL ;
	struct MemTable		*table = NULL ;
	struct MemIndex		*index = NULL ;
	struct UserInfo		user_info ;
	struct timeval		elapse ;
	
	int			nret = 0 ;
	
	printf( "sizeof(struct UserInfo)[%zu]\n" , sizeof(struct UserInfo) );
	printf( "sizeof0(struct UserInfo,email)[%zu]\n" , sizeof0(struct UserInfo,email) );
	printf( "press_count[%d]\n" , press_count );
	
	printf( "MDBCreateDatabase ...\n" );
	db = MDBCreateDatabase( "mydb" ) ;
	if( db == NULL )
	{
		printf( "ERROR : MDBCreateDatabase failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateDatabase ok , db[%p]\n" , db );
	}
	
	printf( "MDBCreateTable mytable ...\n" );
	table = MDBCreateTable( db , "mytable" , sizeof(struct UserInfo) ) ;
	if( table == NULL )
	{
		printf( "ERROR : MDBCreateTable mytable failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateTable mytable ok , table[%p]\n" , table );
	}
	
	printf( "MDBCreateIndex myindex ...\n" );
	index = MDBCreateIndex( table , "myindex" , MDB_UNIQUEINDEX , MDBFIELD(struct UserInfo,uid) , MDBFIELD_NONE ) ;
	if( index == NULL )
	{
		printf( "ERROR : MDBCreateIndex myindex failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateIndex myindex ok , index[%p]\n" , index );
	}
	
	printf( "MDBInsert ...\n" );
	memset( & user_info , 0x00 , sizeof(struct UserInfo) );
	for( user_info.uid = 1 ; user_info.uid <= press_count ; user_info.uid++ )
	{
		snprintf( user_info.user_name , sizeof(user_info.user_name)-1 , "myname-%"PRIu32"" , user_info.uid );
		snprintf( user_info.email , sizeof(user_info.email)-1 , "myname-%"PRIu32"@163.com" , user_info.uid );
		nret = MDBInsert( table , & user_info ) ;
		if( nret )
		{
			printf( "MDBInsert uid[%"PRIu32"] user_name[%s] email[%s] failed[%d]\n" , user_info.uid , user_info.user_name , user_info.email , nret );
			return -1;
		}
		if( press_count/10 > 0 && user_info.uid % (press_count/10) == 0 )
		{
			printf( "MDBInsert uid[%"PRIu32"] user_name[%s] email[%s] ok\n" , user_info.uid , user_info.user_name , user_info.email );
		}
	}
	printf( "MDBInsert all ok , press_count[%d]\n" , press_count );
	
	printf( "MDBSelect ...\n" );
	memset( & user_info , 0x00 , sizeof(struct UserInfo) );
	user_info.uid = 1 ;
	BEGIN_TIMING(elapse)
	printf( "MDBSelect ... , .uid[%"PRIu32"] .user_name[%s] .email[%s]\n" , user_info.uid , user_info.user_name , user_info.email );
	nret = MDBSelect( table , & user_info , MDBFIELD(struct UserInfo,user_name) , MDBFIELD(struct UserInfo,email) , MDBFIELD_NONE , & user_info , MDBFIELD(struct UserInfo,uid) , MDBFIELD_NONE ) ;
	if( nret )
	{
		printf( "ERROR : MDBSelect failed[%d] , .uid[%"PRIu32"] .user_name[%s] .email[%s]\n" , MDBGetLastError() , user_info.uid , user_info.user_name , user_info.email );
		return -1;
	}
	else
	{
		printf( "MDBSelect ok , .uid[%"PRIu32"] .user_name[%s] .email[%s]\n" , user_info.uid , user_info.user_name , user_info.email );
		END_TIMING(elapse)
		printf( "elapse[%ld.%06ld]\n" , elapse.tv_sec , elapse.tv_usec );
	}
	
	printf( "MDBSelect ...\n" );
	memset( & user_info , 0x00 , sizeof(struct UserInfo) );
	snprintf( user_info.email , sizeof(user_info.email)-1 , "myname-%"PRIu32"@163.com" , press_count );
	BEGIN_TIMING(elapse)
	printf( "MDBSelect ... , .uid[%"PRIu32"] .user_name[%s] .email[%s]\n" , user_info.uid , user_info.user_name , user_info.email );
	nret = MDBSelect( table , & user_info , MDBFIELD(struct UserInfo,uid) , MDBFIELD(struct UserInfo,user_name) , MDBFIELD_NONE , & user_info , MDBFIELD(struct UserInfo,email) , MDBFIELD_NONE ) ;
	if( nret )
	{
		printf( "ERROR : MDBSelect failed[%d] , .uid[%"PRIu32"] .user_name[%s] .email[%s]\n" , MDBGetLastError() , user_info.uid , user_info.user_name , user_info.email );
		return -1;
	}
	else
	{
		printf( "MDBSelect ok , .uid[%"PRIu32"] .user_name[%s] .email[%s]\n" , user_info.uid , user_info.user_name , user_info.email );
		END_TIMING(elapse)
		printf( "elapse[%ld.%06ld]\n" , elapse.tv_sec , elapse.tv_usec );
	}
	
	printf( "MDBDropDatabase ...\n" );
	MDBDropDatabase( db );
	printf( "MDBDropDatabase ok\n" );
	
	return 0;
}

int main( int argc , char *argv[] )
{
	if( argc != 1 + 1 )
	{
		printf( "USAGE : press_insert_records press_count\n" );
		exit(1);
	}
	
	return -press_insert_records( (uint32_t)atoi(argv[1]) );
}

